﻿<style>
    .form_title { padding: 10px 50px; margin-bottom: 10px; }
    .form_title b { font-size: 16px; }
    .tbsum { color: #666; font-weight: bold; line-height: 30px; font-size: 20px; width: 100%; margin-left: -2px; margin-top: -2px; border-right: solid 1px #94C0D2; }
    .tbsum tr td { background: #fff; border-left: solid 1px #94C0D2; border-bottom: solid 1px #94C0D2; }
    .divTitle { line-height: 35px; margin-bottom: 5px; padding: 0px 5px; display: none; position: relative; }
    .divTitle button { position: absolute; right: 5px; top: 3px; }
    .divTitle span { margin-right: 30px; }
    .house { width: 200px; }
</style>

<div class="ld-tabs">
    <ul>
        <li class="k-state-active">盘存单</li>
    </ul>
    <div>
        <div class="k-block form_title">
            <div style="display: inline-block;">
                <b>仓库：</b><input id="selHouse" />
            </div>
            <div style="display: inline-block;">
                <b>日期：</b><input id="txtNoteDate_PC" class="date" />
                <b>备注：</b><input id="txtDescription" class="k-textbox" style="width: 400px" />
            </div>
        </div>
        <div id="grid_orders_pc"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("仓库管理", "盘存调拨");

    $(".date").kendoDatePicker({
        value: new Date(),
        culture: "zh-CHS",
        format: "yyyy年MM月dd日",
    });

    $("#selHouse").kendoDropDownList({
        dataTextField: "House",
        dataValueField: "ID",
        dataSource: Whale.dataSource({
            url: "/Products/Warehouse/Gets"
        })
    });
</script>

<script class="grid_orders_columns">
    var orderFields = [{
        title: "产品型号",
        field: "Product",
        template: "#=Product.Module#",
        editor: productEditor,
        width: 200
    }, {
        title: "材料",
        field: "Material",
        template: "#=Material.MaterialName#",
        editor: materialEditor,
        width: 120
    }, {
        title: "加工",
        field: "Process",
        template: "#=Process.ProcessName#",
        editor: processEditor,
        width: 100
    }, {
        title: "规格",
        field: "Size",
        template: "#=Size.SizeName#",
        editor: sizeEditor,
        width: 100
    }, {
        title: "数量",
        field: "Quantity",
        template: "#=QAU(Quantity)#",
        editor: quantityEditor,
        width: 100
    }, {
        title: "操作",
        command: [{
            name: "destroy",
            text: "删除"
        }]
    }];

    function QAU(q) {
        var html = q;
        if (q != "") {
            if ($("#selHouse").data("kendoDropDownList").value() == "2")
                html += " (斤)";
            else
                html += " (只)";
        }
        return html;
    }

    var allProducts = Whale.Remote.GetEntity({
        url: "/Products/Product/Gets",
        parameters: {
            pageSize: 0,
            productStatus: 1
        }
    });
    var materials = Whale.Remote.GetEntity({
        url: "/Products/Material/Gets"
    });
    var processes = Whale.Remote.GetEntity({
        url: "/Products/Process/Gets"
    });
    var sizes = Whale.Remote.GetEntity({
        url: "/Products/Size/Gets"
    });

    function productEditor(container, options) {
        var selPro = $('<input required data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoComboBox({
                autoBind: false,
                filter: "contains",
                dataTextField: "Module",
                dataValueField: "ID",
                dataSource: allProducts,
                change: function (e) {
                    if (selPro.dataItem() == null || selPro.dataItem() == undefined) {
                        $("#grid_orders").data("kendoGrid").cancelRow();
                        return;
                    }
                    var product = selPro.dataItem();
                    options.model.Product = product;

                    if (product.PType == 1) {
                        options.model.Material = { ID: 0, MaterialName: "" };
                        options.model.Process = { ID: 1, ProcessName: "无" };
                        options.model.Size = { ID: 0, SizeName: "无" };

                    } else {
                        options.model.Material = { ID: 0, MaterialName: "" };
                        options.model.Process = { ID: 0, ProcessName: "" };
                        options.model.Size = { ID: 0, SizeName: "" };
                    }

                    $("#grid_orders_pc").data("kendoGrid").refresh();
                }
            }).data("kendoComboBox");
    }
    function materialEditor(container, options) {
        var selMaterial = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "MaterialName",
                dataValueField: "ID",
                dataSource: {
                    transport: {
                        read: function (e) {
                            var ret = $.grep(materials, function (n, i) {
                                return n.MType == options.model.Product.PType;
                            });
                            e.success(ret);
                        }
                    }
                },
                dataBound: function (e) {
                    if (options.model.Material.ID == 0) {
                        options.model.Material = this.dataItem(0);
                    }
                }
            }).data("kendoComboBox");
    }
    function processEditor(container, options) {
        var selProcess = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "ProcessName",
                dataValueField: "ID",
                dataSource: {
                    transport: {
                        read: function (e) {
                            var ret;
                            if (options.model.Product.PType == 1 || $("#selHouse").data("kendoDropDownList").value()==2) {
                                ret = $.grep(processes, function (n, i) {
                                    return n.ID == 1;
                                });
                            } else {
                                ret = $.grep(processes, function (n, i) {
                                    return n.ID > 1;
                                });
                            }
                            e.success(ret);
                        }
                    }
                },
                //dataSource:processes,
                dataBound: function (e) {
                    if (options.model.Process.ID == 0) {
                        options.model.Process = this.dataItem(0);
                    }
                }
            }).data("kendoComboBox");
    }
    function sizeEditor(container, options) {
        var selSize = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "SizeName",
                dataValueField: "SizeName",
                dataSource: {
                    transport: {
                        read: function (e) {
                            var ret = $.grep(sizes, function (n, i) {
                                return n.PType == options.model.Product.PType;
                            });
                            e.success(ret);
                        }
                    }
                },
                //dataSource: {
                //    transport: {
                //        read: function (e) {
                //            var ret;
                //            if (options.model.Product.PType == 1) {
                //                ret = ["无"];
                //            } else {
                //                ret = ["0.25", "0.3", "0.35", "0.4", "0.45", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0"]
                //            }
                //            e.success(ret);
                //        }
                //    }
                //},
                dataBound: function (e) {
                    if (options.model.Size.SizeName == "")
                        options.model.Size = this.dataItem(0);
                    else {

                    }
                }
            }).data("kendoComboBox");
    }
    function quantityEditor(container, options) {
        var txtQuantity = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoNumericTextBox({
                decimals: 3
            }).data("kendoNumericTextBox");
    }
</script>

<script class="grid_orders_pc_config">
    var hasSaved = false;
    var orderPcDataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    Product: { defaultValue: { ID: 0, ProductName: "", UnitName: "" } },
                    Material: { defaultValue: { ID: 0, MaterialName: "" } },
                    Process: { defaultValue: { ID: 0, ProcessName: "" } },
                    Quantity: {},
                    Size: { defaultValue: { ID: 0, SizeName: "" } }
                }
            }
        }
    });

    $("#grid_orders_pc").kendoGrid({
        toolbar: [{
            name: "create",
            text: "新增一行"
        }, {
            name: "save",
            text: "保存盘存单"
        }],
        columns: orderFields,
        pageable: false,
        editable: true,
        dataBound: function (e) {
            //让表头居中
            $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            $("a.k-grid-save-changes").attr("style", "font-weight:bold; float:right");
        },
        dataSource: orderPcDataSource,
        saveChanges: function (e) {
            if (hasSaved) return; else hasSaved = true;
            var values = {
                orderDate: kendo.toString($("#txtNoteDate_PC").data("kendoDatePicker").value(), "yyyy-MM-dd"),
                orderMethod: 13,
                description: $("#txtDescription").val(),
                houseID:$("#selHouse").data("kendoDropDownList").value(),
                orderItems: []
            };

            values.orderItems = $.map(orderPcDataSource.data(), function (o) {
                return {
                    productID: o.Product.ID,
                    materialID: o.Material.ID,
                    processID: o.Process.ID,
                    size: o.Size.SizeName,
                    quantity: o.Quantity
                }
            });
            Whale.Remote.Operation({
                url: "/Orders/ProductOrder/SetBalance",
                parameters: values,
                success: function (result) {
                    location.href = "/Success/PrintProductOrder/" + result.OrderNo;
                },
                error: function (msg) {
                    hasSaved = false;
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    });
</script>
